import time
import os

def test_install_app(driver):
        apk_path = "D:\download\mall.apk"  # 本地APK文件的路径
        if not os.path.exists(apk_path):
            print("文件不存在:"+apk_path)  # 输出: C:\path\with\backslashes
            raise FileNotFoundError(f"文件不存在: {apk_path}")
        push_command = f'adb -s emulator-5554  push "{apk_path}" /data/local/tmp/'
        os.system(push_command)

        output = driver.execute_script("mobile:shell", {
            "command": "pm install -r /data/local/tmp/mall.apk",
            "args": []
        })
        time.sleep(10)  # 假设应用安装需要大约5秒

        try:
            if not driver.is_app_installed("uni.UNI31459F4"):
                print("应用安装失败:")
            else:
                print("应用已安装")
            # 检查应用是否已安装
            assert driver.is_app_installed("uni.UNI31459F4")
        except AssertionError:
            print("应用未安装，断言失败。")

#启动指定设备中的应用
def test_open_app(driver):
    app_package = 'uni.UNI31459F4'
    driver.activate_app(app_package) #成功
    print(f"应用已启动")
    time.sleep(20)  # 等待应用启动

#测试卸载应用
def test_uninstall_app(driver):
    # 定义应用的包名
    package_name = "uni.UNI31459F4"
    # 卸载应用
    driver.remove_app(package_name)
    try:
        if not driver.is_app_installed(package_name):
            print("应用卸载成功:")
        else:
            print("应用卸载失败")
        # 检查应用是否已卸载
        assert not driver.is_app_installed(package_name)
    except AssertionError:
        print("应用卸载失败，断言失败。")    




    